package com.uls.web.controller.ycl;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.ycl.domain.RepairArea;
import com.uls.ycl.service.IRepairAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.InspectionMaintenance;
import com.uls.ycl.service.IInspectionMaintenanceService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 检修维护Controller
 * 
 * @author ycl
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/ycl/maintenance")
public class InspectionMaintenanceController extends BaseController
{
    @Autowired
    private IInspectionMaintenanceService inspectionMaintenanceService;

    @Autowired
    private IRepairAreaService RepairAreaService;

    /**
     * 查询检修维护列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionMaintenance inspectionMaintenance)
    {
        startPage();
        List<InspectionMaintenance> list = inspectionMaintenanceService.selectInspectionMaintenanceList(inspectionMaintenance);

          //通过外键id找名字
        for(InspectionMaintenance maintenance :list){
            Long areaId = maintenance.getAreaId();
            RepairArea repairArea = RepairAreaService.selectRepairAreaByAreaId(areaId);

            if (repairArea!=null){
                maintenance.setName(repairArea.getName());
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出检修维护列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:export')")
    @Log(title = "检修维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionMaintenance inspectionMaintenance)
    {
        List<InspectionMaintenance> list = inspectionMaintenanceService.selectInspectionMaintenanceList(inspectionMaintenance);
        ExcelUtil<InspectionMaintenance> util = new ExcelUtil<InspectionMaintenance>(InspectionMaintenance.class);
        util.exportExcel(response, list, "检修维护数据");
    }

    /**
     * 获取检修维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(inspectionMaintenanceService.selectInspectionMaintenanceById(id));
    }

    /**
     * 新增检修维护
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:add')")
    @Log(title = "检修维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionMaintenance inspectionMaintenance)
    {
        inspectionMaintenance.setCreatedBy(getUsername());
        inspectionMaintenance.setCreatedAt(new Date());
        return toAjax(inspectionMaintenanceService.insertInspectionMaintenance(inspectionMaintenance));
    }

    /**
     * 修改检修维护
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:edit')")
    @Log(title = "检修维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionMaintenance inspectionMaintenance)
    {
        inspectionMaintenance.setUpdatedAt(new Date());
        return toAjax(inspectionMaintenanceService.updateInspectionMaintenance(inspectionMaintenance));
    }

    /**
     * 删除检修维护
     */
    @PreAuthorize("@ss.hasPermi('ycl:maintenance:remove')")
    @Log(title = "检修维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inspectionMaintenanceService.deleteInspectionMaintenanceByIds(ids));
    }
}
